"""
post请求
"""

import urllib.request
import urllib.parse
import json

url = "https://fanyi.baidu.com/sug"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.50"
}

data = {"kw": "spider"}

# post请求的参数，必须要进行编码
data = urllib.parse.urlencode(data).encode("utf-8")
# print(data)

# post的请求参数，是不会拼接在url的后面的，而是需要放在请求对象定制的参数中
# post请求的参数必须进行编码
request = urllib.request.Request(url=url, data=data, headers=headers)
# print(request)

# 模拟游览器向服务器发送请求
response = urllib.request.urlopen(request)
content: bytes = response.read()
print(content.decode("utf-8"))

#
obj = json.loads(content.decode())
print(obj)
